﻿namespace Trader.DataServices.Yahoo
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using BO.Interface;
    using Interface;

    public class SignalsCache : ISignalsCache
    {
        private readonly HistoricalData historicalData;

        public SignalsCache(HistoricalData historicalData)
        {
            this.historicalData = historicalData;
        }

        #region ISignalsCache Members

        public DateTime? FindLastCachedDate(IStock stock, Guid signalID)
        {
            LastSignalsCheck lastSignalCheck =
                historicalData.LastSignalsChecks.Where(
                    instance => (instance.Stock == stock) && (instance.Signal.GUID == signalID)).FirstOrDefault();
            if (lastSignalCheck == null)
            {
                return null;
            }
            return lastSignalCheck.Date;
        }

        public void CacheSignalSimulationResults(IStock stock, Guid signalID, DateTime lastDateChecked,
                                                 IEnumerable<DateTime> actionPoints)
        {
            Signal dbSignal = historicalData.Signals.Where(signal => signal.GUID == signalID).FirstOrDefault();
            if (dbSignal == null)
            {
                dbSignal = new Signal {GUID = signalID};
                historicalData.Signals.InsertOnSubmit(dbSignal);
            }

            historicalData.LastSignalsChecks.InsertOnSubmit(new LastSignalsCheck
                                                                {
                                                                    Signal = dbSignal,
                                                                    Stock = (Stock) stock,
                                                                    Date = lastDateChecked
                                                                });

            IEnumerable<SignalsInstance> instances = from actionPoint in actionPoints
                                                     select new SignalsInstance
                                                                {
                                                                    Date = actionPoint,
                                                                    Recommendation = 1,
                                                                    Signal = dbSignal,
                                                                    Stock = (Stock) stock
                                                                };

            historicalData.SignalsInstances.InsertAllOnSubmit(instances);

            historicalData.SubmitChanges();
        }

        public IEnumerable<DateTime> LoadToDate(IStock stock, Guid signalID, DateTime lastDate)
        {
            IQueryable<DateTime> dates = from instance in historicalData.SignalsInstances
                                         where
                                             instance.Signal.GUID == signalID && instance.Stock == stock &&
                                             instance.Date <= lastDate
                                         select instance.Date;

            return dates;
        }

        #endregion
    }
}